//
//  InfoTableViewController.swift
//  newShaohuoRen
//
//  Created by hujinbiao on 2018/3/23.
//  Copyright © 2018年 hujinbiao. All rights reserved.
//

import UIKit

class InfoTableViewController: UITableViewController {
    
    @IBOutlet weak var headImgView: UIImageView!
    @IBOutlet weak var nickNameLabel: UILabel!
    @IBOutlet weak var nameLabel: UILabel!
    @IBOutlet weak var accountLabel: UILabel!
    @IBOutlet weak var mobileLabel: UILabel!
    @IBOutlet weak var sexLabel: UILabel!
    @IBOutlet weak var autographLabel: UILabel!
    
    var headerImgStr : String?
    var nickNameStr : String?
    var nameStr : String?
    var accountStr : String?
    var mobileStr : String?
    
    var sexStr : String?
    var autographStr : String?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.title = "个人信息"
        
        headImgView.sd_setImage(with: URL(string: headerImgStr ?? ""), placeholderImage: UIImage(named:"icon_default_header_bg"))
        nickNameLabel.text = nickNameStr
        nameLabel.text = nameStr
        accountLabel.text = accountStr
        mobileLabel.text = mobileStr
        sexLabel.text = sexStr
        autographLabel.text = autographStr
    }
    
    func requestUpdateUserInfo(header_img:String) -> Void {
        
        //要加密的数据
        let param = ["command":"updateUserInfo","uid":UserTicketModel.sharedInstance.uid,"token":UserTicketModel.sharedInstance.token,"header_img":header_img,"app_name":"shr"]
        
        NetworkTools.sharedInstance.postRequest(urlString: "", parameters: param as AnyObject, headers: ["copyright":"v1","interface":"UserInfo"], successed: { (resultDict) in
            
            let data = resultDict["data"] as! [String:AnyObject]
            let imageURLStr =  "\(endpoint)/\(data["header_img"]!)"
            self.headImgView.sd_setImage(with: URL(string: imageURLStr), placeholderImage: UIImage(named:"icon_default_header_bg"))
        })
    }
    
    func selectImage() -> Void {
        
        let alert : UIAlertController = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
        alert.addAction(UIAlertAction(title: "从相册选择", style: .default, handler: { (action) in
            let pickerController : UIImagePickerController =  UIImagePickerController()
            pickerController.allowsEditing = true
            pickerController.sourceType = .photoLibrary
            pickerController.delegate = self
            self.present(pickerController, animated: true, completion: nil)
        }))
        alert.addAction(UIAlertAction(title: "拍照", style: .default, handler: { (action) in
            let pickerController : UIImagePickerController =  UIImagePickerController()
            pickerController.allowsEditing = true
            pickerController.sourceType = .camera
            pickerController.delegate = self
            self.present(pickerController, animated: true, completion: nil)
        }))
        alert.addAction(UIAlertAction(title: "取消", style: .cancel, handler: nil))
        self.present(alert, animated: true, completion: nil)
    }
}


extension InfoTableViewController : UIImagePickerControllerDelegate,UINavigationControllerDelegate {
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
        
        let newPhoto = info[UIImagePickerControllerEditedImage] as! UIImage
        picker.dismiss(animated: true, completion: nil)
        //上传图片
        OSSImageUploader.asyncUploadImage(newPhoto, tempFolder: "test") { (name, status) in
            self.requestUpdateUserInfo(header_img:(name?.first)!)
        }
    }
}

extension InfoTableViewController {
    
    override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 20
    }
    
    override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 0.001
    }
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if indexPath.section==0 {
            selectImage()
        }else if indexPath.section==1{
            if indexPath.row==0{
                let board = UIStoryboard(name: "NickViewController", bundle: nil)
                let Vc = board.instantiateInitialViewController() as! NickViewController
                Vc.delegate = self
                Vc.title = "昵称"
                navigationController?.pushViewController(Vc, animated: true)
            }else if indexPath.row==1{
                let board = UIStoryboard(name: "NickViewController", bundle: nil)
                let Vc = board.instantiateInitialViewController() as! NickViewController
                Vc.delegate = self
                Vc.title = "姓名"
                navigationController?.pushViewController(Vc, animated: true)
            }
        }else {
            if indexPath.row==0{
                let board = UIStoryboard(name: "SexViewController", bundle: nil)
                let Vc = board.instantiateInitialViewController() as! SexViewController
                Vc.delegate = self
                Vc.sexStr = sexLabel.text!
                navigationController?.pushViewController(Vc, animated: true)
            }else{
                let board = UIStoryboard(name: "AutographController", bundle: nil)
                let Vc = board.instantiateInitialViewController() as! AutographController
                Vc.delegate = self
                navigationController?.pushViewController(Vc, animated: true)
            }
        }
    }
}

extension InfoTableViewController : NickViewControllerDelegate,SexViewControllerDelegate,AutographControllerDelegate {
    
    func nickViewController(nickName: String, realName: String) {
        nickNameLabel.text = nickName
        nameLabel.text = realName
    }
    
    func sexViewController(sex: String) {
        sexLabel.text = sex
    }
    
    func autographController(signature: String) {
        autographLabel.text = signature
    }
}
